/** @file f_stream.cpp
	@brief Stream interface */

#include "file/f_stream.h"

using namespace G3;

//==============================================================================
// Constructors & Destructors
//==============================================================================
G3::STREAM::STREAM () {
    lastLineDelimiter = '\n';

    flags = 0;

	caretLocked = false;
	bufferReadLocked = false;
	bufferWriteLocked = false;

    log = &G3::MainLog;
}

G3::STREAM::CHUNK::CHUNK () {
    chunkSize = 0;
    chunkPointer = NULL;
}

G3::STREAM::~STREAM () {
    log = NULL;
}

G3::STREAM::CHUNK::~CHUNK () {
    chunkSize = 0;
    chunkPointer = NULL;
}

//==============================================================================
// = operator
//==============================================================================
G3::STREAM::CHUNK& G3::STREAM::CHUNK::operator= (const G3::STREAM::CHUNK &chunk) {
    chunkSize = chunk.chunkSize;
    chunkPointer = chunk.chunkPointer;

    return *this;
}

//==============================================================================
// Enables a flag
//==============================================================================
void G3::STREAM::Enable (ushort flag) {
	flags |= flag;
}

//==============================================================================
// Disables a flag
//==============================================================================
void G3::STREAM::Disable (ushort flag) {
	flags &= (~flag);
}

long G3::GetFileSize (const string &filename) {
    long ret = 0;

	Assert (!filename.empty (), "GetFileSize: Filename argument is a NULL pointer");

    FILE *f = fopen (filename.c_str (), "rb");
    if (f) {
        fseek (f, 0L, SEEK_END);
        ret = ftell (f);
        fclose (f);
    }
    return ret;
}
